'''
Created on 2014-3-4

@author: xizhzhao
'''
from TemplatePageFetcher import *
from core.PageFetcher import  *

'''
    Read me:
        FetcherFactory is used to generate different kinds of page fetchers,
        the url here, you can use :
        str: analyse the netloc of the url to get the corresponding fetcher(Defined in engines.xml)
        tuple: (url, job_site, intent), only generate TemplatePageFetcher
        list: not implemented!
        dict: {url: xxx, controller: xxx, intent}
'''
class FetcherFactory:
    def __init__(self):
        return
    
    def getFetcher(self, url):
        if isinstance(url, str):
            return self.__get_fetcher_from_str(url)
        elif isinstance(url, tuple):
            return self.__get_fetcher_from_tuple(url)
        elif isinstance(url, list):
            return self.__get_fetcher_from_list(url)
        elif isinstance(url, dict):
            return self.__get_fetcher_from_dict(url)
        else:
            return None
    
    def __get_fetcher_from_str(self, url):
        netloc = PageFetcher.get_netloc(url)
        from core.Assigner import Assigner
        controller = Assigner.get_controller(netloc)
        module = __import__("fetchers." + controller)
        module = getattr(module, controller)
        newclass = getattr(module, controller)
        fetcher = newclass()
        fetcher.init(url)
        return fetcher
    
    '''
        Here, let's make a deal. The info structure is (url, job_site, intent).
        The job_site is used as the folder name.
    '''
    def __get_fetcher_from_tuple(self, tpl):
        if len(tpl) == 1:
            return self.__get_fetcher_from_str(tpl)

        fetcher = TemplatePageFetcher()
        if len(tpl) == 2:
            url, job_site = tpl
            # the intent here I suggest is to use dict
            if fetcher.init(url, job_site):
                return fetcher
        elif len(tpl) == 3:
            url, job_site, intent = tpl
            # the intent here I suggest is to use dict
            if fetcher.init(url, job_site, intent):
                return fetcher
        return None
    
    def __get_fetcher_from_list(self, lst):
        return None
    
    def __get_fetcher_from_dict(self, dct):
         try:
            url = dct['url']
            controller = dct['controller']
            intent = False
            if 'intent' in dct.keys():
                intent = dct['intent']
            module = __import__("fetchers." + controller)
            module = getattr(module, controller)
            newclass = getattr(module, controller)
            fetcher = newclass()
            fetcher.init(url, intent)
            return fetcher
         except:
            import sys
            (type, value, tb) = sys.exc_info()
            print str(type)
            print str(value)
            print str(tb)
            import traceback
            traceback.print_exception(type, value, tb)
            logger.error("Encounter exception when __get_fetcher_from_dict: %s " % str(url))
            return None